import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * @Author liangzai
 * @Description:
 */
public class TcpEchoCilent {
    private Socket cilentSocket = null;

    public TcpEchoCilent(String serverIp,int serverport) throws IOException {
        cilentSocket = new Socket(serverIp,serverport);
    }

    public void start(){
        Scanner scanner = new Scanner(System.in);
        try(InputStream inputStream = cilentSocket.getInputStream();
            OutputStream outputStream = cilentSocket.getOutputStream()) {
            Scanner scannerNet = new Scanner(inputStream);
            PrintWriter writer = new PrintWriter(outputStream);
            while(true){
                if(!scanner.hasNext()){
                    break;
                }
                //System.out.print("请输入请求:");
                String request = scanner.nextLine();
                writer.println(request);
                writer.flush();

                String response = scannerNet.nextLine();
                System.out.printf("响应:"+response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {
        TcpEchoCilent cilent = new TcpEchoCilent("127.0.0.1",9090);
        cilent.start();
    }
}
